<?php
/**
 * Created by PhpStorm.
 * User: zhangbing
 * Date: 2017/5/20
 * Time: 16:16
 */
namespace app\admin\controller;
use app\models\Models;
use think\Controller;

/**
 * -----------------------------------
 * Class Base
 * @package app\admin\controller
 * 主要格式化返回信息,和数据验证类
 * ----------------------------------
 */
class Base extends Controller{
    /**
     * ajax请求返回成功信息
     * @param  string $data
     * @return array
     */
    protected static function formatSuccessResult($data = null){
        return self::formatResult(0, '操作成功', $data);
    }
    /**
     * ajax请求返回错误信息
     * @param int $code
     * @param string $errorMsg
     * @param string $data
     * @return array
     */
    protected static function formatResult($code, $errorMsg, $data = null){
        return array('code' => $code,'msg' => $errorMsg,'data'=>$data);
    }

    /**
     * 记录后台操作日志
     * @param string $action
     * @return boolean
     * */
    protected function addLog($action){
        $add['uid']        = cookie('join_login');
        $add['uip']        = ip2long($_SERVER["REMOTE_ADDR"]);
        $add['log_action'] = $action;
        $add['log_time']   = time();
        return db('adminLog')->insert($add);
    }

    /**
     * 获取配置文件
     * @return array
     */
    protected function getConfig(){
        // 获取配置文件信息
        $conf = new Models('config');
        $list = $conf->_select();
        $config = [];
        if(!empty($list)){
            foreach($list as $v){
                $config[$v['key']] = $v['value'];
            }
        }
        return $config;
    }

    /**
     * 获取当前用户的类型
     * @return mixed
     */
    protected function getGroup(){
        $id  = cookie('join_login');
        $group = new Models('auth_group_access');
        $group_id = $group->getValue(['uid'=>$id],'group_id');
        return ['id'=>$id,'group_id'=>$group_id];
    }


    /**
     * 数组映射组合现场图片url
     * @param $image
     * @return string
     */
    function arrayMapLive($image){
        $config = self::getConfig();
        return [
            'ori'=>$config['http'].$_SERVER['HTTP_HOST']. DS .'uploads'. DS .'live'. DS .$image,
            'thumb'=>$config['http'].$_SERVER['HTTP_HOST']. DS .'uploads'. DS .'live'. DS .'s'.$image
        ];
    }

    /**
     * 数组映射组合动态图片url
     * @param $image
     * @return string
     */
    function arrayMapDynamic($image){
        $config = self::getConfig();
        return [
            'ori'=>$config['http'].$_SERVER['HTTP_HOST']. DS .'uploads'. DS .'dynamic'. DS .$image,
            'thumb'=>$config['http'].$_SERVER['HTTP_HOST']. DS .'uploads'. DS .'dynamic'. DS .'s'.$image
        ];
    }
}